package yhao.infra.common;

import org.springframework.boot.context.properties.ConfigurationProperties;
import redis.clients.jedis.Protocol;
import yhao.infra.common.cache.cachekey.CacheKeyType;

/**
 * @author yoara
 * <p> Created on 2019/8/12 </p>
 */
@ConfigurationProperties(prefix = YhaoInfraCommonProperties.PREFIX)
public class YhaoInfraCommonProperties {
    public static final String PREFIX = "yhao.infra.common";

    public static final String REDIS_ENABLE = PREFIX+".redis-enable";
    public static final String ENV_APP_NAME = "spring.application.name";
    public static final String ENV_DEPLOY = PREFIX+".env.deploy";

    /** 是否将session_base标志的存储key{@link CacheKeyType#SESSION_BASE}，放置在公共命名空间区域**/
    private boolean useRedisSessionBase = true;

    /** redis是否启用**/
    private boolean redisEnable = true;

    /** 读配置使用与写配置一致的参数**/
    private boolean queryUseSaveProperties = false;

    /** 写redis相关配置 **/
    private Redis saveRedis = new Redis();

    /** 读redis相关配置 **/
    private Redis queryRedis = new Redis();

    /** 应用环境相关配置 **/
    private Env env = new Env();

    public Redis getSaveRedis() {
        return saveRedis;
    }

    public void setSaveRedis(Redis saveRedis) {
        this.saveRedis = saveRedis;
    }

    public Redis getQueryRedis() {
        return queryRedis;
    }

    public void setQueryRedis(Redis queryRedis) {
        this.queryRedis = queryRedis;
    }

    public Env getEnv() {
        return env;
    }

    public void setEnv(Env env) {
        this.env = env;
    }

    public boolean isRedisEnable() {
        return redisEnable;
    }

    public void setRedisEnable(boolean redisEnable) {
        this.redisEnable = redisEnable;
    }

    public boolean isQueryUseSaveProperties() {
        return queryUseSaveProperties;
    }

    public void setQueryUseSaveProperties(boolean queryUseSaveProperties) {
        this.queryUseSaveProperties = queryUseSaveProperties;
    }

    public boolean isUseRedisSessionBase() {
        return useRedisSessionBase;
    }

    public void setUseRedisSessionBase(boolean useRedisSessionBase) {
        this.useRedisSessionBase = useRedisSessionBase;
    }

    public static class Redis {
        /**redis连接池最大连接数量**/
        private int poolMaxTotal = 100;

        /**redis连接池最大闲置数量**/
        private int poolMaxIdle = 5;

        /**redis连接池最大闲置时间**/
        private int poolMaxWaitMillis = 20000;

        /**redis的url配置**/
        private String url;

        /**redis的端口配置，如果是集群设置，则不需要填写这一项**/
        private int port = Protocol.DEFAULT_PORT;

        /**redis的密码**/
        private String password;

        /**使用redis数据库Index**/
        private int dbIndex = Protocol.DEFAULT_DATABASE;

        /**使用redis超时时间**/
        private int timeout = Protocol.DEFAULT_TIMEOUT;

        /**是否使用集群设置**/
        private boolean clusterEnable = false;

        public int getPoolMaxTotal() {
            return poolMaxTotal;
        }

        public void setPoolMaxTotal(int poolMaxTotal) {
            this.poolMaxTotal = poolMaxTotal;
        }

        public int getPoolMaxIdle() {
            return poolMaxIdle;
        }

        public void setPoolMaxIdle(int poolMaxIdle) {
            this.poolMaxIdle = poolMaxIdle;
        }

        public int getPoolMaxWaitMillis() {
            return poolMaxWaitMillis;
        }

        public void setPoolMaxWaitMillis(int poolMaxWaitMillis) {
            this.poolMaxWaitMillis = poolMaxWaitMillis;
        }

        public int getDbIndex() {
            return dbIndex;
        }

        public void setDbIndex(int dbIndex) {
            this.dbIndex = dbIndex;
        }

        public boolean isClusterEnable() {
            return clusterEnable;
        }

        public void setClusterEnable(boolean clusterEnable) {
            this.clusterEnable = clusterEnable;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public int getPort() {
            return port;
        }

        public void setPort(int port) {
            this.port = port;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public int getTimeout() {
            return timeout;
        }

        public Redis setTimeout(int timeout) {
            this.timeout = timeout;
            return this;
        }

        @Override
        public String toString() {
            return "Redis{" +
                    "poolMaxTotal=" + poolMaxTotal +
                    ", poolMaxIdle=" + poolMaxIdle +
                    ", poolMaxWaitMillis=" + poolMaxWaitMillis +
                    ", url='" + url + '\'' +
                    ", port=" + port +
                    ", password='" + password + '\'' +
                    ", dbIndex=" + dbIndex +
                    ", timeout=" + timeout +
                    ", clusterEnable=" + clusterEnable +
                    '}';
        }
    }

    public static class Env {
        /**环境**/
        private String deploy;
        /**
         * 应用名<br>
         * 配置了应用名的数据会流入该命名空间内，否则流进默认的命名空间CLOUD_ANONYMOUS
         * **/
        private String appName;

        public String getDeploy() {
            return deploy;
        }

        public void setDeploy(String deploy) {
            this.deploy = deploy;
        }

        public String getAppName() {
            return appName;
        }

        public void setAppName(String appName) {
            this.appName = appName;
        }
    }


}
